//
//  WmcEnvironmentTemplateView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/6/13.
//

import UIKit

/// 环境治理
class WmcEnvironmentTemplateView: WmcBaseWatermarkTemplateView {
    override func config() {
        super.config()
        defaultWidth = 225.wmc_autoHor

        backgroundColorView.layer.cornerRadius = 4
        backgroundColorView.layer.masksToBounds = true
        backgroundColorView.backgroundColor = UIColor.black
        addSubview(backgroundColorView)

        stackView.axis = .vertical
        stackView.distribution = .equalSpacing
        stackView.alignment = .fill
        stackView.spacing = 0
        addSubview(stackView)
    }

    override func configWatermarkAlpha(_ alpha: Int) {
        let alpha = calculateAlpha(alpha)
        backgroundColorView.alpha = alpha
    }

    override func configWatermarkModel(_ watermarkModel: WmcWatermarkDetailModel, styleMode: WmcBaseWatermarkTemplateView.StyleMode) {
        super.configWatermarkModel(watermarkModel, styleMode: styleMode)

        var drawViews: [UIView] = []
        // 获取标题最大字数
        let maxTitleCount = watermarkModel.fieldTitleMaxCount(filters: [.sub_title])
        watermarkModel.fields.forEach { option in
            guard option.isOpen else { return }
            switch option.fieldType {
            case .title: // 标题
                let item = createDefaultTextItem(option: option,
                                                 maxTitleCount: maxTitleCount)
                drawViews.append(item)

            case .sub_title: // 子标题
                let item = createDefaultTextItem(option: option,
                                                 font: 15.yp.uiFont(weight: .bold))
                drawViews.append(item)

            case .time: // 时间
                let item = createDateItem(option: option,
                                          styleMode: styleMode,
                                          staticDate: watermarkModel.staticData.staticDate,
                                          maxTitleCount: maxTitleCount,
                                          dateFormat: .yyyy_MM_dd_HH_mm_week)
                drawViews.append(item)

            case .addr: // 地点
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .location)
                drawViews.append(item)

            case .lat: // 经纬
                let item = createLocationItem(option: option,
                                              styleMode: styleMode,
                                              staticAddress: watermarkModel.staticData.drawStaticAddress,
                                              maxTitleCount: maxTitleCount,
                                              locationFormat: .longitudLatitude)
                drawViews.append(item)

            case .weather: // 天气
                let item = createWeatherItem(option: option,
                                             styleMode: styleMode,
                                             staticWeater: watermarkModel.staticData.staticWeater,
                                             maxTitleCount: maxTitleCount,
                                             weatherFormat: .weatherTemperature)
                drawViews.append(item)

            case .altitude: // 海拔
                let item = createAltitudeItem(option: option,
                                              styleMode: styleMode,
                                              staticLocation: watermarkModel.staticData.staticLocation,
                                              maxTitleCount: maxTitleCount,
                                              altitudeFormat: .meters)
                drawViews.append(item)

            case .azimuth: // 方位角
                let item = createAzimuthItem(option: option,
                                             styleMode: styleMode,
                                             staticAzimuth: watermarkModel.staticData.staticAzimuth,
                                             maxTitleCount: maxTitleCount,
                                             azimuthFormat: .default)
                drawViews.append(item)

            case .decibe: // 分贝
                let item = createDecibeItem(option: option,
                                            styleMode: styleMode,
                                            maxTitleCount: maxTitleCount,
                                            decibeFormat: .describeDB)
                drawViews.append(item)

            case .speed: // 速度
                let item = createSpeedItem(option: option,
                                           styleMode: styleMode,
                                           staticLocation: watermarkModel.staticData.staticLocation,
                                           maxTitleCount: maxTitleCount,
                                           speedFormat: .km_h)
                drawViews.append(item)

            case .remark,
                 .record,
                 .text,
                 .custom: // 备注、记工、输入
                let item = createDefaultTextItem(option: option,
                                                 maxTitleCount: maxTitleCount)
                drawViews.append(item)

            case .brand: // 品牌图
                configOptionBrand(option)

            default: break
            }
        }

        drawViews.forEach { view in
            setShadow(view)
        }
        // 添加进堆栈视图
        stackView.yp.arrangedSubviews = drawViews
    }

    override func makeConstraints() {
        super.makeConstraints()

        backgroundColorView.snp.makeConstraints { make in
            make.top.left.equalTo(8)
            make.right.equalTo(-8)
            if isShowCertificationWatermark {
                make.bottom.equalTo(certificationView.snp.top).offset(-4)
            } else {
                make.bottom.equalToSuperview().offset(-8)
            }
        }

        stackView.snp.makeConstraints { make in
            make.top.right.equalTo(backgroundColorView)
            make.left.equalTo(backgroundColorView)
            if isShowCertificationWatermark {
                make.bottom.equalTo(certificationView.snp.top).offset(-4)
            } else {
                make.bottom.equalToSuperview().offset(-8)
            }
        }
    }
}
